package me.xiaosiqi.web.controller.store;

import com.github.pagehelper.PageInfo;
import me.xiaosiqi.domain.store.Catalog;
import me.xiaosiqi.domain.store.Company;
import me.xiaosiqi.domain.store.Question;
import me.xiaosiqi.service.store.CompanyService;
import me.xiaosiqi.utils.BeanUtil;
import me.xiaosiqi.utils.FileUtil;
import me.xiaosiqi.web.controller.BaseServlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author xiaosiqi
 * @date 2021/6/3 18:01
 */

@WebServlet("/store/question")
public class QuestionServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/javascript;charset=utf-8");
        String operation = req.getParameter("operation");
        System.out.println(operation);
        if ("list".equals(operation)){
            // uri:/store/question?operation=list
            list(req,resp);
        }else if ("delete".equals(operation)){
            // uri:/store/question?operation=delete
            delete(req,resp);
        }else if ("toAdd".equals(operation)){
            toAdd(req,resp);
        }else if ("save".equals(operation)){
            try {
                save(req,resp);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            try {
                edit(req,resp);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
        }else if ("toExport".equals(operation)){
            toExport(req,resp);
        }

    }

    private void toExport(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //模板excel文件的位置
        String realPath = this.getServletContext().getRealPath("/");
        questionService.getReport(realPath);
        req.setAttribute("uri","../template/question.xlsx");
        req.getRequestDispatcher("/WEB-INF/pages/store/question/download.jsp").forward(req,resp);
    }


    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Question question = questionService.findById(id);
        req.setAttribute("question",question);
        List<Company> companyList = companyService.findAll();
        req.setAttribute("companyList",companyList);
        List<Catalog> catalogList = catalogService.findAll();
        req.setAttribute("catalogList",catalogList);
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/store/question/update.jsp").forward(req,resp);
    }
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException, FileUploadException {
        //创建磁盘工厂对象
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //Servlet文件上传核心对象
        ServletFileUpload fileUpload = new ServletFileUpload(factory);
        //从request中读取数据
        List<FileItem> fileItems = fileUpload.parseRequest(req);
        String RealPath = this.getServletContext().getRealPath("/");
        questionService.update(fileItems,RealPath);
        resp.sendRedirect(req.getContextPath()+"/store/question?operation=list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //跳转页面
        List<Company> companyList = companyService.findAll();
        req.setAttribute("companyList",companyList);
        List<Catalog> catalogList = catalogService.findAll();
        req.setAttribute("catalogList",catalogList);
        req.getRequestDispatcher("/WEB-INF/pages/store/question/add.jsp").forward(req,resp);

    }
    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException, FileUploadException {
        if (ServletFileUpload.isMultipartContent(req)){
            //创建磁盘工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //Servlet文件上传核心对象
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            //从request中读取数据
            List<FileItem> fileItems = fileUpload.parseRequest(req);
            String RealPath = this.getServletContext().getRealPath("/");
            questionService.save(fileItems,RealPath);
        }
        resp.sendRedirect(req.getContextPath()+"/store/question?operation=list");
    }




    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Question question = new Question();
        question.setId(id);
        String realPath = this.getServletContext().getRealPath("/");
        questionService.delete(question,realPath);
        resp.sendRedirect(req.getContextPath()+"/store/question?operation=list");

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 15;
       if (StringUtils.isNoneBlank(req.getParameter("page"))){
           page = Integer.parseInt(req.getParameter("page"));
       }
        if (StringUtils.isNoneBlank(req.getParameter("size"))){
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo pageInfo = questionService.findPage(page, size);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/store/question/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
